package com.loiot.baqi.service.job;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.loiot.baqi.commons.message.email.EmailClient;
import com.loiot.baqi.commons.message.email.SimpleEmailVo;
import com.loiot.baqi.constant.ApplicationConst;
import com.loiot.baqi.constant.Const;
import com.loiot.baqi.dao.ZpCompanyJobInfoDao;
import com.loiot.baqi.pojo.ZpCompanyJobInfo;
import com.loiot.baqi.status.AccountType;
import com.loiot.baqi.status.CompanyJobSourceType;
import com.loiot.baqi.status.HeartbeatJobType;
import com.loiot.baqi.status.PauseStartType;
import com.loiot.baqi.utils.HtmlUnitUtil;
import com.loiot.baqi.utils.JsonToObject;
import com.loiot.baqi.utils.JsonUtils;
import com.loiot.baqi.utils.MD5Util;
import com.loiot.baqi.vo.hd.Root;
import com.loiot.commons.utils.DateUtil;
import com.loiot.commons.utils.StringUtil;

@Service("statisticsPlatformData")
@Scope("prototype")
public class StatisticsPlatformData extends JobTaskDefine {

	@Resource
	private ZpCompanyJobInfoDao zpCompanyJobInfoDao;
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public void doTask() {
		statisticsPlatformData();
	}
	
	
	public void statisticsPlatformData(){
		HashMap<String, Object> pmap = new HashMap<String, Object>();
		StringBuffer buff = new StringBuffer();
		try {
			buff.append(queryUserInfo());
			buff.append(queryJlInfo());
			buff.append(queryFlowInfo());
			buff.append(this.queryCompanyInfo());
			buff.append(this.queryCompanyJobInfo());
			/*if(1==1){
				System.out.println(buff);
				return;
			}*/
			//.
			//发送股东邮件
			SimpleEmailVo vo = new SimpleEmailVo();
			List<String> stockholds = StringUtil.splitToString(ApplicationConst.STOCKHOLDER_MAILS, ",");
			for(String stockholdMail :stockholds){
				vo.addEmail(stockholdMail);
			}
            vo.setTitle("线上数据统计");
            vo.setContent("近一周数据统计：<br><br>"+buff.toString().replace("\n", ""));
            emailClient.sendT(vo);
            log.info("发送时间："+DateUtil.toString(DateUtil.getNow(), DateUtil.DEFAULT_LONG_FORMAT)); 

			//System.out.println("buff:"+buff);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.exceptionContent=e.getMessage();
		}finally{
			//添加心跳
			this.jobType=(int)HeartbeatJobType.STATISTICS_PLATFORM_DATA.getCode();
			this.addHeartbeat();
		}
	}
	/**
	 * 统计用户信息
	 * @throws Exception
	 */
	public StringBuffer queryUserInfo() throws Exception{
		StringBuffer buff = new StringBuffer();
		
		HashMap<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("qtype",Const.ONE);
		//pmap.put("type",Const.ONE);
		List<HashMap<String, Object>> list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("总用户："+this.getValue(list.get(0),"count")+"个 \n<br>");
		
		//hr
		pmap.put("type",AccountType.HR.getCode());
		pmap.putAll(this.getDateTimeParam());
		list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("新增hr："+this.getValue(list.get(0),"count")+"个 \n<br>");
		
		
		//技术评审
		pmap.put("type",AccountType.TECHICAL_AUDIT.getCode());
		list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("新增技术评审："+this.getValue(list.get(0),"count")+"个\n<br>");
		
		//System.out.println(buff);
		return buff;
	}
	
	/**
	 * 统计求职者信息
	 * @throws Exception
	 */
	public StringBuffer queryJlInfo() throws Exception{
		StringBuffer buff = new StringBuffer();
		
		HashMap<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("qtype",Const.TWO);
		pmap.putAll(this.getDateTimeParam());

		List<HashMap<String, Object>> list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("上传简历："+this.getValue(list.get(0),"jl_count")+"个 \n<br>");
		
		pmap.put("startTimeT", "FFF");
		list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);

		buff.append("评审通过："+this.getValue(list.get(0),"pass")+"个 \n<br>");
		buff.append("评审未通过："+this.getValue(list.get(0),"no_pass")+"个 \n<br>");
		
		//System.out.println(buff);
		return buff;
	}
	
	
	/**
	 * 统计流程信息
	 * @throws Exception
	 */
	public StringBuffer queryFlowInfo() throws Exception{
		StringBuffer buff = new StringBuffer();
		
		HashMap<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("qtype",Const.THREE);
		pmap.putAll(this.getDateTimeParam());
		List<HashMap<String, Object>> list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("已推荐到企业待企业回复："+this.getValue(list.get(0), "ALREADY_RECOMMEND_COMPANY")+"个 \n<br>");
		buff.append("推荐企业没通过："+this.getValue(list.get(0), "RECOMMEND_COMPANY_FAILURE")+"个 \n<br>");
		buff.append("求职者未同意面试："+this.getValue(list.get(0), "JOBHUNTER_NO_AGREE_INTERVIEW")+"个 \n<br>");
		buff.append("求职者已经去面试："+this.getValue(list.get(0), "HUNTER_ALREAD_GOTO_INTERVIEW")+"个 \n<br>");
		buff.append("求职者未去面试："+this.getValue(list.get(0), "HUNTER_NO_GOTO_INTERVIEW")+"个 \n<br>");
		buff.append("面试已通过："+this.getValue(list.get(0), "HUNTER_INTERVIEW_PASS")+"个 \n<br>");
		buff.append("面试未通过："+this.getValue(list.get(0), "HUNTER_INTERVIEW_NO_PASS")+"个 \n<br>");

		//System.out.println(buff);
		return buff;
	}
	
	
	/**
	 * 查询企业信息
	 * @throws Exception
	 */
	public StringBuffer queryCompanyInfo() throws Exception{
		StringBuffer buff = new StringBuffer();
		
		HashMap<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("qtype",Const.FOUR);
		pmap.put("isDelete",PauseStartType.START.getCode());
		pmap.putAll(this.getDateTimeParam());
		List<HashMap<String, Object>> list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("新增企业："+this.getValue(list.get(0), "count")+"个 \n<br>");

		return buff;
	}
	
	/**
	 * 查询职位信息
	 * @throws Exception
	 */
	public StringBuffer queryCompanyJobInfo() throws Exception{
		StringBuffer buff = new StringBuffer();
		
		HashMap<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("qtype",Const.FIVE);
		pmap.put("isDelete",PauseStartType.START.getCode());
		List<HashMap<String, Object>> list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("正在招聘的职位："+this.getValue(list.get(0), "count")+"个 \n<br>");
		
		pmap.putAll(this.getDateTimeParam());
		pmap.remove("isDelete");
		list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("添加职位："+this.getValue(list.get(0), "count")+"个 \n<br>");

		
		pmap.put("isDelete",PauseStartType.PAUSE.getCode());
		list = zpCompanyJobInfoDao.statisticsPlatformData(pmap);
		buff.append("暂停职位："+this.getValue(list.get(0), "count")+"个 \n<br>");

		return buff;
	}
	
	
	
	public String getValue(HashMap<String, Object> mapObj,String key){
		if(mapObj==null || mapObj.get(key)==null){
			return "0";
		}
		return mapObj.get(key).toString();
	}
	
	
	
	public HashMap<String, Object> getDateTimeParam(){
		HashMap<String, Object> pMap = new HashMap<String, Object>();
    	pMap.put("startTime",DateUtil.addDays(new Date(), -7));
		pMap.put("endTime",new Date());
    	return pMap;
	}
	
	
	
}
